/*
 * Copyright 2013 JROSE
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jrose.webapp.controller;

import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.jrose.entity.constant.Gender;
import org.jrose.entity.constant.Race;
import org.jrose.service.user.UserSignupContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * User controller implementation.
 *
 * @author Xiaohai Zhang
 * @since 2013-09-05 21:51
 */
@Controller
@RequestMapping("/")
@NoArgsConstructor
public class UserController extends AbstractController {
    private static final List<Gender> availableGenders;
    private static final List<Race> availableRaces;

    static {
        availableGenders = new LinkedList<>();
        availableRaces = new LinkedList<>();
        availableGenders.addAll(Arrays.asList(Gender.values()));
        availableRaces.addAll(Arrays.asList(Race.values()));
        for (Iterator<Race> it = availableRaces.iterator(); it.hasNext(); ) {
            Race race = it.next();
            if (!race.isAvailable()) {
                it.remove();
            }
        }
    }

    @RequestMapping(value = "index.shtml", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "signup.shtml", method = RequestMethod.GET)
    public String signup(Model model) {
        model.addAttribute("genders", availableGenders);
        model.addAttribute("races", availableRaces);
        return "signup";
    }

    @RequestMapping(value = "signup.shtml", method = RequestMethod.POST)
    public String signupUser(Model model) {
        Gender gender = Gender.valueOf(requestParameter("gender"));
        Race race = Race.valueOf(requestParameter("race"));
        String username = requestParameter("username");
        String password = requestParameter("password");
        String repeatPassword = requestParameter("repeatPassword");
        String rolename = requestParameter("rolename");

        if (!StringUtils.equals(password, repeatPassword)) {
            model.addAttribute("genders", availableGenders);
            model.addAttribute("races", availableRaces);
            return "signup";
        }

        UserSignupContext context = new UserSignupContext();
        context.setGender(gender);
        context.setRace(race);
        context.setUsername(username);
        context.setPassword(password);
        context.setRolename(rolename);
        userService.signup(context);

        return "signin";
    }

    @RequestMapping(value = "signin.shtml", method = RequestMethod.GET)
    public String signin() {
        return "signin";
    }
}
